package com.znsd.zl.collect.controller;

import com.znsd.zl.browse.entity.vo.TBrowseEntityVo;
import com.znsd.zl.collect.entity.CollectEntity;
import com.znsd.zl.collect.service.CollectService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CollectController
 * @Description 收藏controller层
 * @author xyx
 * @date 2020-08-28 14:48:33
 * @Version v1.0
 */
@RestController
@RequestMapping("/zl/collect")
public class CollectController extends BaseController <CollectService, CollectEntity, Integer> {

	@PostMapping("/saveCollect")
	public R saveCollect(@RequestBody CollectEntity entity) {
			try {
				/*判断登录用户Id是否为null,为null则为游客登录,不进行收藏*/
				if(entity.getUserId()!=null){
					return service.collectSave(entity) ? R.ok() : R.error("添加失败");
				}else {
					return R.error();
				}
			}catch (Exception e){
				e.printStackTrace();
				return R.error();
			}
	}

	@GetMapping("/list")
	public R list(Integer userId){
		try{
			List<TBrowseEntityVo> list = service.findList(userId);
			service.findImages(19);
			return R.ok().put("list",list);
		}catch(Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@PostMapping("/status")
	public boolean findStatus(@RequestBody Map map){
		System.out.println("进入Status");
		CollectEntity entity=new CollectEntity();
		String start="null";
		String userIds=String.valueOf(map.get("userId"));
		String houseIds=String.valueOf(map.get("houseId"));
		if(start.equals(userIds)||start.equals(houseIds)){
			return false;
		}else{
			int userId = Integer.parseInt(String.valueOf(map.get("userId")));
			int houseId = Integer.parseInt(String.valueOf(map.get("houseId")));
			entity.setUserId(userId);
			entity.setHouseId(houseId);
			boolean status = service.findStatus(entity);
			return status;
		}
	}

	@DeleteMapping("/deleteCollect")
	public R deleteCollect(@RequestBody Map map) {
		try {
			CollectEntity entity=new CollectEntity();
			int userId = Integer.parseInt(String.valueOf(map.get("userId")));
			int houseId = Integer.parseInt(String.valueOf(map.get("houseId")));
			entity.setUserId(userId);
			entity.setHouseId(houseId);
			service.deleteCollect(entity);
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
	}
}
